package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysDict;
import com.linq.cool.system.dto.SysDictQueryDTO;
import com.linq.cool.system.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/23 00:39
 * @Description: 数据字典信息
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("dict")
public class SysDictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 条件分页获取字典信息
     *
     * @param dto 数据字典信息
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:dict:list")
    @GetMapping("list")
    public Result<?> list(SysDictQueryDTO dto) {
        log.info("SysDictQueryDTO: {}", dto);
        IPage<SysDict> page = sysDictService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 根据字典编码查询
     *
     * @param dictCode 字典编码
     *
     * @return 结果
     */
    @GetMapping("code/{dictCode}")
    public Result<?> getDictByCode(@PathVariable("dictCode") String dictCode) {
        return Result.ok(sysDictService.getOne(new LambdaQueryWrapper<SysDict>().eq(SysDict::getCode, dictCode)));
    }

    /**
     * 根据ID查询字典详细信息
     *
     * @param dictid 字典id
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:dict:query")
    @GetMapping("{dictId}")
    public Result<?> getInfo(@PathVariable("dictId") Long dictid) {
        return Result.ok(sysDictService.getById(dictid));
    }


    /**
     * 新增字典
     *
     * @param dict 字典信息
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典信息", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "sys:dict:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysDict dict) {
        if (sysDictService.checkCodeUnique(dict)) {
            return Result.fail("新增字典'" + dict.getName() + "'失败，字典编码'" + dict.getCode() + "'已存在");
        }
        dict.setCreator(SecurityUtils.getUserId());
        return this.toResult(sysDictService.saveDict(dict));
    }

    /**
     * 修改字典
     *
     * @param dict 字典信息
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典信息", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:dict:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysDict dict) {
        if (sysDictService.checkCodeUnique(dict)) {
            return Result.fail("修改字典'" + dict.getName() + "'失败，字典编码'" + dict.getCode() + "'已存在");
        }
        dict.setGmtModified(new Date()).setModifier(SecurityUtils.getUserId());
        return this.toResult(sysDictService.updateDict(dict));
    }

    /**
     * 根据id批量删除字典
     *
     * @param dictIds 字典id
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典信息", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "sys:dict:remove")
    @DeleteMapping("{dictIds}")
    public Result<?> remove(@PathVariable("dictIds") List<Long> dictIds) {
        return this.toResult(sysDictService.deleteByIds(dictIds));
    }

    /**
     * 清空缓存
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典信息", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "sys:dict:remove")
    @DeleteMapping("/clearCache")
    public Result<?> clearCache() {
        sysDictService.clearCache();
        return Result.ok();
    }

    /**
     * 获取字典选择框列表
     *
     * @return 结果
     */
    @GetMapping("getOptionSelect")
    public Result<?> getOptionSelect() {
        return Result.ok(sysDictService.list());
    }
}
